package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.UserMapper;
import org.example.pojo.PageBean;
import org.example.pojo.User;
import org.example.pojo.UserDTO;
import org.example.service.UserService;
import org.example.util.Md5Util;
import org.example.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public User login(UserDTO user) {
        log.info("用户信息：{}", user);
        if (user.getType().equals("管理员") && !user.getUsername().equals("admin")){
            throw new RuntimeException("当前用户不是管理员！！！");
        }
        if (user.getType().equals("普通员工") && user.getUsername().equals("admin")){
            throw new RuntimeException("当前用户不是普通用户！！！");
        }
        User user2 = userMapper.getByUsername(user.getUsername());
        //查询当前用户的状态
        if (user2.getStatus().equals("禁用")){
            throw new RuntimeException("当前用户已禁用！！！");
        }
//        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        User user1 = userMapper.login(user);
        if (user1 == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        return user1;
    }

    /**
     * 获取用户信息
     * @return
     */
    @Override
    public User getInfo() {
        // 从线程中获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int)map.get("id");
        return userMapper.getInfo(id);
    }

    /**
     * 获取用户列表
     * @return
     */
    @Override
    public PageBean<User> list(Integer pageNum, Integer pageSize, String status, String inputName) {
        PageHelper.startPage(pageNum, pageSize);
        Page<User> page = userMapper.list(status, inputName);
        return new PageBean<>(page.getTotal(), page.getResult());
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public void add(User user) {
        // 根据username查询用户是否存在，如果存在，抛出异常
        User user1 = userMapper.getByUsername(user.getUsername());
        if (user1 != null) {
            throw new RuntimeException("用户已存在，请重新输入用户名！！");
        }
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //  密码加密
//        String md5String = Md5Util.getMD5String(user.getPassword());
//        user.setPassword(md5String);
        user.setPassword(user.getPassword());
        userMapper.add(user);
    }

    /**
     * 修改用户
     * @param user
     */
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
//        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        user.setPassword(user.getPassword());
        userMapper.update(user);
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    /**
     * 修改头像
     * @param avatarUrl
     */
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int)map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
    }

    /**
     * 修改密码
     * @param map
     */
    @Override
    public void updatePwd(Map<String, String> map) {
        String oldPwd = map.get("old_pwd"); // 旧密码
        String newPwd = map.get("new_pwd"); // 新密码
        Map<String, Object> map2 = ThreadLocalUtil.get();
        int id = (int)map2.get("id");
        User user = userMapper.getById(id);
//        oldPwd = Md5Util.getMD5String(oldPwd);
        if (!user.getPassword().equals(oldPwd)){
            throw new RuntimeException("旧密码输入错误！！");
        }
//        newPwd = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(newPwd, id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        for (Integer id : ids){
            userMapper.delete(id);
        }
    }

}
